const API = require('../../wxapi/main.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    article: [],
    buyNumber: 1,
    shopCarInfo: {},
    shopNum: 0
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    // 获取购物车数据
    wx.getStorage({
      key: 'shopCarInfo',
      success: function(res) {
        that.setData({
          shopCarInfo: res.data,
          shopNum: res.data.shopNum,
          curuid: wx.getStorageSync('uid')
        });
      }
    })
  },
  /**
   * 自定义方法
   */
  numJianTap: function(){
    if(this.data.buyNumber > 1){
      let crrueBuy = this.data.buyNumber;
      crrueBuy--;
      this.setData({buyNumber: crrueBuy});
    }
  },
  numJiaTap: function(){
    let crrueBuy = this.data.buyNumber;
    crrueBuy++;
    this.setData({buyNumber: crrueBuy});
  },
  // 去结算
  goShopCar: function(){
    wx.reLaunch({
      url: '/pages/cart/cart',
    })
  },
  /**
   * 组建购物车信息
   */
  bulidShopCarInfo: function() {
    // 加入购物车
    var shopCarMap = {};
    shopCarMap.goodsId = this.data.article.basicInfo.id;
    shopCarMap.pic = this.data.article.basicInfo.pic;
    shopCarMap.name = this.data.article.basicInfo.name;
    shopCarMap.propertyChildIds = this.data.propertyChildIds;
    shopCarMap.label = this.data.propertyChildNames;
    shopCarMap.price = this.data.article.basicInfo.minPrice;
    shopCarMap.left = "";
    shopCarMap.active = true;
    shopCarMap.number = this.data.buyNumber;
    shopCarMap.logisticsType = this.data.article.basicInfo.logisticsId;
    shopCarMap.logistics = this.data.article.logistics;
    shopCarMap.weight = this.data.article.basicInfo.weight;

    var shopCarInfo = this.data.shopCarInfo;
    if (!shopCarInfo.shopNum) {
      shopCarInfo.shopNum = 0;
    }
    if (!shopCarInfo.shopList) {
      shopCarInfo.shopList = [];
    }
    var hasSameGoodsIndex = -1;
    for (var i = 0; i < shopCarInfo.shopList.length; i++) {
      var tmpShopCarMap = shopCarInfo.shopList[i];
      if (tmpShopCarMap.goodsId == shopCarMap.goodsId && tmpShopCarMap.propertyChildIds == shopCarMap.propertyChildIds) {
        hasSameGoodsIndex = i;
        shopCarMap.number = shopCarMap.number + tmpShopCarMap.number;
        break;
      }
    }

    shopCarInfo.shopNum = shopCarInfo.shopNum + this.data.buyNumber;
    if (hasSameGoodsIndex > -1) {
      shopCarInfo.shopList.splice(hasSameGoodsIndex, 1, shopCarMap);
    } else {
      shopCarInfo.shopList.push(shopCarMap);
    }
    shopCarInfo.kjId = this.data.kjId;
    return shopCarInfo;
  },
  // 加入购物车
  addShopCar: function(){
    //组建购物车
    var shopCarInfo = this.bulidShopCarInfo();
    console.log(shopCarInfo);
    this.setData({
      shopCarInfo: shopCarInfo,
      shopNum: shopCarInfo.shopNum
    });

    // 写入本地存储
    wx.setStorage({
      key: 'shopCarInfo',
      data: shopCarInfo,
      success: function(){
        wx.showToast({
          title: '加入购物车成功',
          icon: 'success'
        })
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this;
    API.goodArticle({
      id:options.id
    }).then(res => {
      if(res.code == 0){
        _this.setData({article: res.data});
      }
      console.log(res.data)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})